<!--
 * @Description: HlsPlayer 播放器
 * @Author: chendingfengmail@163.com
 * @Date: 2021-11-19 19:52:15
 * @LastEditTime: 2022-01-11 11:16:28
 * @LastEditors: chendingfengmail@163.com
 * @Reference: 
-->
<template>
  <div class="player" id="dplayer" ref="player" @click="playerClick"></div>
</template>

<script>
import Hls from 'hls.js'
import DPlayer from 'dplayer';
export default {
  name: "ddHlsPlayer",
  data() {
    return {
      hls: {},
    }
  },
  props: {
    title: {
      type: String,
      default: ''
    },
    index: {
      type: String,
      default: ''
    }
  },
  mounted() {
    this.removeMenu()
  },
  methods: {
    loadVideo(url) {
      let _this = this;
      this.dp = new DPlayer({
        element: this.$refs.player,
        autoplay: true,
        live: true,
        mutex: false,
        contextmenu: [
          {
            text: '移除',
            click: (player) => {
              _this.destroyVideo()
              this.$emit('remove-video', player)
            },
          },
        ],
        video: {
          // pic: videoInfo.img, // 封面
          url: url,
          type: 'customHls',
          customType: {
            customHls: function (video, player) {
              _this.hls = new Hls()
              _this.hls.loadSource(video.src)
              _this.hls.attachMedia(video)
            }
          }
        }
      })
      this.removeMenu()
    },

    // 摧毁播放器
    destroyVideo() {
      if (this.dp) {
        this.dp.pause()
        this.hls.destroy()
        this.dp.destroy()
      }
    },


    // 处理视频暂停问题
    playerClick() {
      if (this.dp) {
        this.dp.play()
      }
    },

    // 去除右键版权菜单
    removeMenu() {
      this.$nextTick(() => {
        let dplayerItems = document.querySelectorAll(".dplayer-menu-item")
        for (const key in dplayerItems) {
          if (typeof dplayerItems[key] == "object") {
            let aDom = dplayerItems[key].querySelector("a")
            let innerTexts = ["视频统计信息", "关于作者", "DPlayer v1.26.0"]
            if (innerTexts.includes(aDom.innerHTML)) {
              dplayerItems[key].style.display = "none";
            }
          }
        }
        
      })
    }

  }
}
</script>

<style scoped>
.player {
  background-color: black;
  height: 100%;
  border: 1px solid white;
  color: white;
  text-align: center;
}
::v-deep .dplayer-live-badge {
  display: none;
}
::v-deep .dplayer-bezel-icon {
  display: none;
}
::v-deep .dplayer-icons-left {
  display: none;
}
</style>